package edu.cs.usask.ca.silver.plugin.rcp.dialogs.simulations;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

/**
 * this class implements the Radio Button group that get user to choose specific
 * simulations
 * 
 * @author Yudi Xue
 */
public class RunAnylogicSimulationDialog extends Dialog {

	private static int flag = 0;
	private Button baselineSimButton;
	private Button screenShotSimButton;
	private Button logSimButton;
	private static Logger log = Logger.getLogger(RunAnylogicSimulationDialog.class);
	public static final int NORMAL_SIM = 1;
	public static final int SCREENSHOT_SIM = 2;
	public static final int LOGGING_SIM = 3;
	//private Button testSimButton;
	private static Logger logger = Logger.getLogger(RunAnylogicSimulationDialog.class);

	public RunAnylogicSimulationDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Choose execution options");
	}

	/**
	 * a method that creates the dialog box that allows the user to change any
	 * model object name This is generic for all info editors that are created
	 */
	@Override
	protected Control createDialogArea(Composite parent) {

		// TODO figure out how to set the size of this dialog box

		final Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(1, true);
		
		//TODO use enum
		layout.horizontalSpacing = 8;
		layout.marginBottom = 4;
		layout.marginTop = 4;
		layout.marginLeft = 6;
		layout.marginRight = 6;
		composite.setLayout(layout);

		Listener listener = new Listener() {
			public void handleEvent(Event e) {
				doSelection((Button) e.widget);
			}
		};

		baselineSimButton = new Button(composite, SWT.RADIO);
		baselineSimButton.setText("Run the baseline scenario of this ModelVersion");
		baselineSimButton
				.setToolTipText("Just run the baseline simulation");
		baselineSimButton.addListener(SWT.SELECTED, listener);
		baselineSimButton.setSelection(true);

		screenShotSimButton = new Button(composite, SWT.RADIO);
		screenShotSimButton
				.setText("Run current scenario");
		screenShotSimButton
				.setToolTipText("Run a simulation with parameter values specified in this scenario");
		screenShotSimButton.addListener(SWT.SELECTED, listener);

		logSimButton = new Button(composite, SWT.RADIO);
		logSimButton.setVisible(false);
		logSimButton
				.setText("Run a normal scenario and log changes of state variables");
		logSimButton
				.setToolTipText("take a live logging of the state variables to the simulation");
		logSimButton.addListener(SWT.SELECTED, listener);


		return composite;

	}/* end of control method */

	static void doSelection(Button button) {
		if (button.getSelection()) {
			log.info("do work for selection " + button);
		} else {
			log.info("do work for deselection " + button);
		}
	}

	/**
	 * 
	 * TODO implement this
	 */
	@Override
	protected void okPressed() {
		if (baselineSimButton.getSelection()) {
			logger.info("do work for normal sim");
			flag = NORMAL_SIM;
		}
		if (screenShotSimButton.getSelection()) {
			logger.info("do work for screenshot");
			flag = SCREENSHOT_SIM;
		}
		if (logSimButton.getSelection()) {
			logger.info("do work for logging sim");
			flag = LOGGING_SIM;
		}

		super.okPressed();
	}

	/**
	 * return the selection from Radio Button <br>
	 * <br>
	 * 1 -> normal simulation <br>
	 * 2 -> simulation with screenshot<br>
	 * 3 -> logging simulation<br>
	 * 4 -> testing simulation<br>
	 * 
	 * @return value between 1 - 4
	 */
	public int selection() {
		return flag;

	}/* end of get name method */
}